<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Flutter," />










<meta name="description" content="1.Flutter入口 在lib下的main().dart中，使用runApp进行加载  引入包（输入fim快速引入） 12import &amp;#x27;dart:io&amp;#x27;;import &amp;#x27;package:flutter&#x2F;material.dart&amp;#x27;;  加载主函数 12345void main() &amp;#123;  runApp(MyApp);&amp;#125;&#x2F;&#x2F;等价于void">
<meta property="og:type" content="article">
<meta property="og:title" content="flutter基本组件的使用">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="1.Flutter入口 在lib下的main().dart中，使用runApp进行加载  引入包（输入fim快速引入） 12import &amp;#x27;dart:io&amp;#x27;;import &amp;#x27;package:flutter&#x2F;material.dart&amp;#x27;;  加载主函数 12345void main() &amp;#123;  runApp(MyApp);&amp;#125;&#x2F;&#x2F;等价于void">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/liqin.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_static.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_for.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_server.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_builder1.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_builder2.png">
<meta property="article:published_time" content="2022-03-15T12:48:29.000Z">
<meta property="article:modified_time" content="2022-03-18T12:20:39.067Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="Flutter">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/liqin.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/03/15/flutter基本组件的使用/"/>





  <title>flutter基本组件的使用 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">flutter基本组件的使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-03-15T20:48:29+08:00">
                2022-03-15
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/03/15/flutter基本组件的使用/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-Flutter入口"><a href="#1-Flutter入口" class="headerlink" title="1.Flutter入口"></a>1.Flutter入口</h2><ul>
<li><p>在lib下的main().dart中，使用runApp进行加载</p>
</li>
<li><p>引入包（输入fim快速引入）</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;dart:io&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:flutter/material.dart&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>加载主函数</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">  runApp(MyApp);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//等价于</span></span><br><span class="line"><span class="keyword">void</span> main()=&gt;runApp(MyAPP())</span><br></pre></td></tr></table></figure>

<span id="more"></span></li>
<li><p>把runApp里面的参数抽离出来变成一个组件，MyApp 继承StatelessWidget方法，且需要实现抽象方法build</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyApp</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span></span>&#123;</span><br><span class="line">  <span class="keyword">const</span> MyApp(&#123;Key? key&#125;) : <span class="keyword">super</span>(key: key);</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> MaterialApp(</span><br><span class="line">      home: Scaffold(</span><br><span class="line">        appBar: AppBar(</span><br><span class="line">          title: Text(<span class="string">&quot;Flutter Demo!&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        body: HomeContent(),</span><br><span class="line">      ),</span><br><span class="line">      theme: ThemeData(</span><br><span class="line">        primarySwatch: Colors.green</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span></span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span>  <span class="keyword">const</span> Center(</span><br><span class="line">      child: Text(</span><br><span class="line">        <span class="string">&quot;Hello,Flutter=====&quot;</span>,</span><br><span class="line">        textDirection: TextDirection.ltr,</span><br><span class="line">        textAlign: TextAlign.left,</span><br><span class="line">        style: TextStyle(</span><br><span class="line">          fontSize: <span class="number">20.0</span>,</span><br><span class="line">          color: Colors.blue</span><br><span class="line">        )</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>在这里，MaterialApp组件非常重要，表示一般作为根组件进行使用，里面需要传入home参数，home用Scaffold进行装饰，Scaffold里面需要传入appBar和body,body可以有其他组件抽离而成</p>
</li>
<li><p>可以把MyApp当成一个固定的写法，后期只需要在body里面进行更改就行了</p>
</li>
</ul>
</li>
</ul>
<h2 id="2-Container组件"><a href="#2-Container组件" class="headerlink" title="2.Container组件"></a>2.Container组件</h2><ul>
<li><p>是一个容器组件，类似于前端的div</p>
</li>
<li><p>child就是孩子结点，还可以继续放其他组件</p>
</li>
<li><p>container的一些属性</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span></span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> Center(</span><br><span class="line">      child: Container(</span><br><span class="line">        <span class="comment">// ignore: prefer_const_constructors</span></span><br><span class="line">        child: Text(</span><br><span class="line">          <span class="string">&#x27;我是一个文本&#x27;</span>,</span><br><span class="line">          textAlign: TextAlign.center,</span><br><span class="line">          <span class="comment">// ignore: prefer_const_constructors</span></span><br><span class="line">          style: TextStyle(</span><br><span class="line">            letterSpacing: <span class="number">5.0</span></span><br><span class="line">          ),</span><br><span class="line">          ),</span><br><span class="line">        height: <span class="number">300.0</span>,</span><br><span class="line">        width: <span class="number">300.0</span>,</span><br><span class="line">        decoration: BoxDecoration(</span><br><span class="line">          color: Colors.lightBlue,</span><br><span class="line">          border: Border.all(</span><br><span class="line">            color: Colors.lightGreen,</span><br><span class="line">            width: <span class="number">5.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          borderRadius: BorderRadius.all(</span><br><span class="line">            Radius.circular(<span class="number">20.0</span>),</span><br><span class="line">          )</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="3-Flutter引入本地图片"><a href="#3-Flutter引入本地图片" class="headerlink" title="3.Flutter引入本地图片"></a>3.Flutter引入本地图片</h2><ul>
<li><p>在根目录下新建<code>images</code>文件夹，在images文件夹下新建<code>2.0x</code>,<code>3.0x</code>,<code>4.0x</code>,把相应的文件资源放在对应的文件夹下面，主要是针对不同分辨率的屏幕进行适配</p>
</li>
<li><p>编辑<code>pubspec.yaml</code>文件，把**asset: **结点展开，把目录配置进去，比如：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># The following section is specific to Flutter.</span></span><br><span class="line"><span class="attr">flutter:</span></span><br><span class="line">  <span class="comment"># The following line ensures that the Material Icons font is</span></span><br><span class="line">  <span class="comment"># included with your application, so that you can use the icons in</span></span><br><span class="line">  <span class="comment"># the material Icons class.</span></span><br><span class="line">  <span class="attr">uses-material-design:</span> <span class="literal">true</span></span><br><span class="line">  <span class="comment"># To add assets to your application, add an assets section, like this:</span></span><br><span class="line">  <span class="attr">assets:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">images/liqin.jpeg</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">images/2.0x/liqin.jpeg</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">images/3.0x/liqin.jpeg</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">images/4.0x/liqin.jpeg</span></span><br></pre></td></tr></table></figure></li>
<li><p>在相应的组件中进行引入图片：</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> Center(</span><br><span class="line">      child: Container(</span><br><span class="line">      child: Image.asset(</span><br><span class="line">        <span class="string">&quot;images/liqin.jpeg&quot;</span>,</span><br><span class="line">        width: <span class="number">400</span>,</span><br><span class="line">        height: <span class="number">600</span>,</span><br><span class="line">        fit: BoxFit.fill,</span><br><span class="line">      ),</span><br><span class="line">      decoration: BoxDecoration(color: Colors.blueGrey),</span><br><span class="line">    ));</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/liqin.png" class title="李沁">

<p>如果卡在 <code>running &quot; flutter pub get &quot; in XXX </code>，那么检查一下镜像配置是否有问题，建议去看看环境变量是否增加了</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置 用户变量</span></span><br><span class="line"></span><br><span class="line">FLUTTER_STORAGE_BASE_URL：https:<span class="comment">//storage.flutter-io.cn</span></span><br><span class="line">PUB_HOSTED_URL：https:<span class="comment">//pub.flutter-io.cn</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//重启AndroidStudio或者VScode</span></span><br></pre></td></tr></table></figure>

<p>如果环境变量正常，请检查网络状态</p>
</li>
</ul>
<h2 id="4-Flutter的列表组件"><a href="#4-Flutter的列表组件" class="headerlink" title="4.Flutter的列表组件"></a>4.Flutter的列表组件</h2><ul>
<li>大家都知道，列表组件在安卓开发中是非常重要的，flutter提供了4种列表组件</li>
</ul>
<ol>
<li>垂直列表</li>
<li>垂直图文列表</li>
<li>水平列表</li>
<li>动态列表</li>
<li>矩阵式列表</li>
</ol>
<h3 id="1-垂直列表"><a href="#1-垂直列表" class="headerlink" title="1.垂直列表"></a>1.垂直列表</h3><ul>
<li><p>使用ListView</p>
</li>
<li><p>垂直列表</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span></span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView(</span><br><span class="line">      <span class="comment">//表示配置它的子元素</span></span><br><span class="line">      <span class="comment">// ignore: prefer_const_literals_to_create_immutables</span></span><br><span class="line">      children: [</span><br><span class="line">        <span class="comment">//一般配合ListTile来使用的</span></span><br><span class="line">        ListTile(</span><br><span class="line">          <span class="comment">//添加图标</span></span><br><span class="line">          leading: Icon(</span><br><span class="line">            Icons.access_time_outlined,</span><br><span class="line">            size: <span class="number">40.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          <span class="comment">// ignore: unnecessary_const</span></span><br><span class="line">          title: Text(<span class="string">&quot;本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">          subtitle: Text(<span class="string">&quot;原标题：本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        ListTile(</span><br><span class="line">          leading: Icon(</span><br><span class="line">            Icons.access_time_outlined,</span><br><span class="line">            size: <span class="number">40.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          <span class="comment">// ignore: unnecessary_const</span></span><br><span class="line">          title: Text(<span class="string">&quot;本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">          subtitle: Text(<span class="string">&quot;原标题：本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        ListTile(</span><br><span class="line">          leading: Icon(</span><br><span class="line">            Icons.access_time_outlined,</span><br><span class="line">            size: <span class="number">40.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          <span class="comment">// ignore: unnecessary_const</span></span><br><span class="line">          title: Text(<span class="string">&quot;本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">          subtitle: Text(<span class="string">&quot;原标题：本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        ListTile(</span><br><span class="line">          leading: Icon(</span><br><span class="line">            Icons.access_time_outlined,</span><br><span class="line">            size: <span class="number">40.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          <span class="comment">// ignore: unnecessary_const</span></span><br><span class="line">          title: Text(<span class="string">&quot;本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">          subtitle: Text(<span class="string">&quot;原标题：本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        ListTile(</span><br><span class="line">          leading: Icon(</span><br><span class="line">            Icons.access_time_outlined,</span><br><span class="line">            size: <span class="number">40.0</span>,</span><br><span class="line">          ),</span><br><span class="line">          <span class="comment">// ignore: unnecessary_const</span></span><br><span class="line">          title: Text(<span class="string">&quot;本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">          subtitle: Text(<span class="string">&quot;原标题：本土新增“3507+1647”，其中吉林省“3076+991”&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">      ],</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-垂直图文列表"><a href="#2-垂直图文列表" class="headerlink" title="2.垂直图文列表"></a>2.垂直图文列表</h3><ul>
<li><p>图文列表和其他列表没什么区别，在<code>children</code>属性里面接收一个数组，可以放一个<code>Image</code>和一个<code>Container</code>构成图文列表</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView(</span><br><span class="line">      children: [</span><br><span class="line">        Container(</span><br><span class="line">          child: Text(</span><br><span class="line">            <span class="string">&quot;我是李沁&quot;</span>,</span><br><span class="line">            textAlign: TextAlign.center,</span><br><span class="line">            style: TextStyle(fontSize: <span class="number">20</span>),</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">        Image.asset(</span><br><span class="line">          <span class="string">&quot;images/liqin.jpeg&quot;</span>,</span><br><span class="line">          width: <span class="number">400</span>,</span><br><span class="line">          height: <span class="number">600</span>,</span><br><span class="line">          fit: BoxFit.fill,</span><br><span class="line">        ),</span><br><span class="line">        Container(</span><br><span class="line">          child: Text(</span><br><span class="line">            <span class="string">&quot;我是李沁&quot;</span>,</span><br><span class="line">            textAlign: TextAlign.center,</span><br><span class="line">            style: TextStyle(fontSize: <span class="number">20</span>),</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">        Image.asset(</span><br><span class="line">          <span class="string">&quot;images/liqin.jpeg&quot;</span>,</span><br><span class="line">          width: <span class="number">400</span>,</span><br><span class="line">          height: <span class="number">600</span>,</span><br><span class="line">          fit: BoxFit.fill,</span><br><span class="line">        ),</span><br><span class="line">        Container(</span><br><span class="line">          child: Text(</span><br><span class="line">            <span class="string">&quot;我是李沁&quot;</span>,</span><br><span class="line">            textAlign: TextAlign.center,</span><br><span class="line">            style: TextStyle(fontSize: <span class="number">20</span>),</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">        Image.asset(</span><br><span class="line">          <span class="string">&quot;images/liqin.jpeg&quot;</span>,</span><br><span class="line">          width: <span class="number">400</span>,</span><br><span class="line">          height: <span class="number">600</span>,</span><br><span class="line">          fit: BoxFit.fill,</span><br><span class="line">        ),</span><br><span class="line">        Container(</span><br><span class="line">          child: Text(</span><br><span class="line">            <span class="string">&quot;我是李沁&quot;</span>,</span><br><span class="line">            textAlign: TextAlign.center,</span><br><span class="line">            style: TextStyle(fontSize: <span class="number">20</span>),</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">        Image.asset(</span><br><span class="line">          <span class="string">&quot;images/liqin.jpeg&quot;</span>,</span><br><span class="line">          width: <span class="number">400</span>,</span><br><span class="line">          height: <span class="number">600</span>,</span><br><span class="line">          fit: BoxFit.fill,</span><br><span class="line">        ),</span><br><span class="line">      ],</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-水平列表"><a href="#3-水平列表" class="headerlink" title="3.水平列表"></a>3.水平列表</h3><ul>
<li><p>在children属性下面添加属性值<em>scrollDirection</em>: Axis.horizontal,就可以修改成水平列表</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scrollDirection: Axis.horizontal,</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="4-动态列表"><a href="#4-动态列表" class="headerlink" title="4.动态列表"></a>4.动态列表</h3><p>两种实现方式</p>
<ol>
<li>循环语句实现</li>
<li>ListView提供的ListView.builder实现</li>
</ol>
<p>实现一个静态的列表</p>
<ul>
<li><p>定义一个方法，返回一个数组，把这个数组给children就可以</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line"><span class="comment">// 自定义一个方法，返回一个数组，把这个数组给children就可以</span></span><br><span class="line"><span class="comment">//只有当前类可以使用，定义成私有的方法</span></span><br><span class="line">  <span class="built_in">List</span>&lt;Widget&gt; _getData() &#123;</span><br><span class="line">    <span class="keyword">return</span> [</span><br><span class="line">      ListTile(</span><br><span class="line">        title: Text(<span class="string">&quot;我是一个列表1&quot;</span>),</span><br><span class="line">      ),</span><br><span class="line">      ListTile(</span><br><span class="line">        title: Text(<span class="string">&quot;我是一个列表2&quot;</span>),</span><br><span class="line">      ),</span><br><span class="line">      ListTile(</span><br><span class="line">        title: Text(<span class="string">&quot;我是一个列表3&quot;</span>),</span><br><span class="line">      ),</span><br><span class="line">      ListTile(</span><br><span class="line">        title: Text(<span class="string">&quot;我是一个列表4&quot;</span>),</span><br><span class="line">      ),</span><br><span class="line">    ];</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView(</span><br><span class="line">      children: <span class="keyword">this</span>._getData(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_static.png" class title="静态列表 通过数组返回"></li>
</ul>
<h4 id="1-循环语句实现"><a href="#1-循环语句实现" class="headerlink" title="1.循环语句实现"></a>1.循环语句实现</h4><ul>
<li><p>动态使用循环语句添加</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line"><span class="comment">// 自定义一个方法，返回一个数组，把这个数组给children就可以</span></span><br><span class="line"><span class="comment">//只有当前类可以使用，定义成私有的方法</span></span><br><span class="line">  <span class="built_in">List</span>&lt;Widget&gt; _getData() &#123;</span><br><span class="line">    <span class="comment">// ignore: unnecessary_new</span></span><br><span class="line">    <span class="built_in">List</span>&lt;Widget&gt; list = [];</span><br><span class="line">      </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">20</span>; i++) &#123;</span><br><span class="line">      list.add(ListTile(</span><br><span class="line">        title: Text(<span class="string">&quot;我是第<span class="subst">$i</span>个列表&quot;</span>),</span><br><span class="line">      ));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> list;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView(</span><br><span class="line">      children: _getData(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_for.png" class title="循环语句添加List"></li>
<li><p>若有一个json数据是这样的</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">List listData = [</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">1</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Candy shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Mohamed&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>: <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/cat.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Childhood in a picture&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Google&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>: <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/my.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Alibaba Shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Alibaba&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>:</span><br><span class="line">        <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/background.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">4</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Candy Shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Mohamed&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>:</span><br><span class="line">        <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/background1.jpg&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">];</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>首先先将json格式的数据用map方法转换成list数组</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line"><span class="comment">// 自定义一个方法，返回一个数组，把这个数组给children就可以</span></span><br><span class="line"><span class="comment">//只有当前类可以使用，定义成私有的方法</span></span><br><span class="line">  <span class="built_in">List</span>&lt;Widget&gt; _getData() &#123;</span><br><span class="line">    <span class="keyword">var</span> tempList = listData.map((value) &#123;</span><br><span class="line">      <span class="keyword">return</span> ListTile(</span><br><span class="line">        leading: Image.network(value[<span class="string">&quot;imageUrl&quot;</span>]),</span><br><span class="line">        title: Text(value[<span class="string">&quot;title&quot;</span>]),</span><br><span class="line">        subtitle: Text(value[<span class="string">&quot;author&quot;</span>]),</span><br><span class="line">      );</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">return</span> tempList.toList();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView(</span><br><span class="line">      children: _getData(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_server.png" class title="服务器返回的数据进行解析"></li>
</ul>
<h4 id="2-ListView-Builder实现"><a href="#2-ListView-Builder实现" class="headerlink" title="2.ListView.Builder实现"></a>2.ListView.Builder实现</h4><ul>
<li><p>先自定义数据实现</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="built_in">List</span> list = [];</span><br><span class="line">    <span class="comment">//构造函数</span></span><br><span class="line">  HomeContent() &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">20</span>; i++) &#123;</span><br><span class="line">      <span class="keyword">this</span>.list.add(<span class="string">&quot;我是第<span class="subst">$i</span>个列表&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView.builder(</span><br><span class="line">      itemCount: list.length,</span><br><span class="line">      itemBuilder: (context, index) &#123;</span><br><span class="line">        <span class="keyword">return</span> ListTile(</span><br><span class="line">          title: Text(list[index]),</span><br><span class="line">        );</span><br><span class="line">      &#125;,</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_builder1.png" class title="list_builder循环遍历数据"></li>
<li><p>返回json数据的实现</p>
</li>
<li><p>json数据如下</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">List listData = [</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">1</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Candy shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Mohamed&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>: <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/cat.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Childhood in a picture&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Google&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>: <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/my.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Alibaba Shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Alibaba&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>:</span><br><span class="line">        <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/background.jpg&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">&quot;id&quot;</span>: <span class="number">4</span>,</span><br><span class="line">    <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Candy Shop&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;Mohamed&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;imageUrl&quot;</span>:</span><br><span class="line">        <span class="string">&quot;http://chaoyxy.cn/data/User/admin/home/desktop/images/background1.jpg&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">];</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>解析</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="comment">//自定义方法，返回类型是Widget（组件）</span></span><br><span class="line">  Widget _getListData(context, index) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListTile(</span><br><span class="line">        leading: Image.network(listData[index][<span class="string">&quot;imageUrl&quot;</span>]),</span><br><span class="line">        title: Text(listData[index][<span class="string">&quot;title&quot;</span>]),</span><br><span class="line">        subtitle: Text(listData[index][<span class="string">&quot;author&quot;</span>]));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView.builder(</span><br><span class="line">        itemCount: listData.length, itemBuilder: _getListData);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/list_builder2.png" class title="list_builder循环遍历数据"></li>
<li><p>所有代码</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ignore_for_file: prefer_const_constructors, avoid_unnecessary_containers, deprecated_member_use</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:flutter/material.dart&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:myflutterapp/res/listData.dart&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">  runApp(MyApp());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyApp</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> MyApp(&#123;Key? key&#125;) : <span class="keyword">super</span>(key: key);</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> MaterialApp(</span><br><span class="line">      home: Scaffold(</span><br><span class="line">        appBar: AppBar(</span><br><span class="line">          title: Text(<span class="string">&quot;flutter demo&quot;</span>),</span><br><span class="line">        ),</span><br><span class="line">        body: HomeContent(),</span><br><span class="line">      ),</span><br><span class="line">      theme: ThemeData(primarySwatch: Colors.blue),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ignore: use_key_in_widget_constructors</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeContent</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="comment">//自定义方法，返回类型是Widget（组件）</span></span><br><span class="line">  Widget _getListData(context, index) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListTile(</span><br><span class="line">        leading: Image.network(listData[index][<span class="string">&quot;imageUrl&quot;</span>]),</span><br><span class="line">        title: Text(listData[index][<span class="string">&quot;title&quot;</span>]),</span><br><span class="line">        subtitle: Text(listData[index][<span class="string">&quot;author&quot;</span>]));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> ListView.builder(</span><br><span class="line">        itemCount: listData.length, itemBuilder: _getListData);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/Flutter/" rel="tag"># Flutter</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/03/13/Vue%E5%8F%8C%E5%90%91%E7%BB%91%E5%AE%9A%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86/" rel="next" title="Vue双向绑定底层原理">
                <i class="fa fa-chevron-left"></i> Vue双向绑定底层原理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/03/17/%E6%89%8B%E6%92%95vue%E4%B9%8B%E5%AF%B9%E4%BA%8B%E4%BB%B6%E6%8C%82%E8%BD%BD%E5%92%8C%E5%8F%8C%E5%90%91%E7%BB%91%E5%AE%9A%E5%8E%9F%E7%90%86%E8%AF%A6%E8%A7%A3/" rel="prev" title="手撕vue之对事件挂载原理详解">
                手撕vue之对事件挂载原理详解 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-Flutter%E5%85%A5%E5%8F%A3"><span class="nav-number">1.</span> <span class="nav-text">1.Flutter入口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-Container%E7%BB%84%E4%BB%B6"><span class="nav-number">2.</span> <span class="nav-text">2.Container组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-Flutter%E5%BC%95%E5%85%A5%E6%9C%AC%E5%9C%B0%E5%9B%BE%E7%89%87"><span class="nav-number">3.</span> <span class="nav-text">3.Flutter引入本地图片</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-Flutter%E7%9A%84%E5%88%97%E8%A1%A8%E7%BB%84%E4%BB%B6"><span class="nav-number">4.</span> <span class="nav-text">4.Flutter的列表组件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%9E%82%E7%9B%B4%E5%88%97%E8%A1%A8"><span class="nav-number">4.1.</span> <span class="nav-text">1.垂直列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%9E%82%E7%9B%B4%E5%9B%BE%E6%96%87%E5%88%97%E8%A1%A8"><span class="nav-number">4.2.</span> <span class="nav-text">2.垂直图文列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%B0%B4%E5%B9%B3%E5%88%97%E8%A1%A8"><span class="nav-number">4.3.</span> <span class="nav-text">3.水平列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%8A%A8%E6%80%81%E5%88%97%E8%A1%A8"><span class="nav-number">4.4.</span> <span class="nav-text">4.动态列表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.4.1.</span> <span class="nav-text">1.循环语句实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-ListView-Builder%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.4.2.</span> <span class="nav-text">2.ListView.Builder实现</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/03/15/flutter%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/';
          this.page.identifier = '2022/03/15/flutter基本组件的使用/';
          this.page.title = 'flutter基本组件的使用';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
